package com.liangzhong.model.youngTrain
{
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.youngTrain.vo.PlayerChildInfoVo;
	import com.liangzhong.model.youngTrain.vo.YoungTrainInfoVo;
	
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *青训营服务器数据返回解析  
	 * @author lake
	 * 
	 */	
	public class YoungTrainServerDataParse
	{
		public function YoungTrainServerDataParse()
		{
		}
		/**
		 * 读取升级返回数据
		 * @param data
		 * {index:1,pid:1,growbegin:10000,grow:200}
		 */		
		public static function readGradeUp(data : ByteArray):Object
		{
			var resObj :Object = new Object();
			resObj.retcode 		= data.readInt();//返回码
			if(resObj.retcode == 0)
			{
				var baseInfo :YoungTrainInfoVo = AppData.getInstance().youngTrainManager.youngTrainInfoVo;
				baseInfo.level 			= data.readByte();
				baseInfo.coin 			= data.readUnsignedInt();
				baseInfo.fame 			= data.readInt();
				baseInfo.caddienum 		= data.readByte();
				baseInfo.numcashpoint 	= data.readByte();
				baseInfo.caddiecount 	= data.readInt();
				baseInfo.caddie			= data.readInt();
				var len :int 	= data.readShort();
				var obj :Object;
				for(var i :int = 0;i < len ;i++)
				{
					obj = new PlayerChildInfoVo();
					obj.index 		= data.readByte();
					obj.pid 		= data.readInt();
					obj.growbegin 	= data.readUnsignedInt();
					obj.grow 		= data.readInt();
					if(obj.pid > 0)
					{
						AppData.getInstance().youngTrainManager.addPlayerChild(obj);
					}
				}
				var curtime :Number = AppData.getInstance().time;
				baseInfo.pretime = curtime;
				Alert.showErrorTip("升级成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(resObj.retcode) + "");
			}
			return resObj;
		}
		/**
		 * 读取添加球童返回数据
		 * @param data
		 */		
		public static function readAddPlayer(data : ByteArray):Object
		{
			var resObj :Object = new Object();
			resObj.retcode 		= data.readInt();//返回码
			if(resObj.retcode == 0)
			{
				var baseInfo :YoungTrainInfoVo = AppData.getInstance().youngTrainManager.youngTrainInfoVo;
				baseInfo.numcashpoint ++;
				var obj :Object = new PlayerChildInfoVo();
				obj.index 		= data.readByte();
				obj.pid 		= data.readInt();
				obj.growbegin 	= data.readUnsignedInt();
				obj.grow 		= data.readInt();
				if(obj.pid > 0)
				{
					AppData.getInstance().youngTrainManager.addPlayerChild(obj);
				}
				Alert.showErrorTip("增加球童成功");
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(resObj.retcode) + "");
			}
			return resObj;
		}
		/**
		 * 读取签约球童返回数据
		 * @param data
		 */		
		public static function readGetPlayer(data : ByteArray):Object
		{
			var resObj :Object = new Object();
			resObj.retcode 		= data.readInt();//返回码
			var indexs :Array = AppData.getInstance().youngTrainManager.saleOrgetChildIndexs;
			if(resObj.retcode == 0)
			{
				var baseInfo :YoungTrainInfoVo = AppData.getInstance().youngTrainManager.youngTrainInfoVo;
				baseInfo.caddie = data.readInt();
				var obj :Object = {};
				var len :int 	= data.readShort();
				var stars :Array = [];
				for(var i :int = 0;i < len ;i++)
				{
					obj = new PlayerChildInfoVo();
					obj.index 		= data.readByte();
					stars.push(data.readInt());
					obj.pid 		= data.readInt();
					obj.growbegin 	= data.readUnsignedInt();
					obj.grow 		= data.readInt();
					if(obj.pid > 0)
					{
						indexs.splice(indexs.indexOf(obj.index,1));
						AppData.getInstance().youngTrainManager.addPlayerChild(obj);
					}
				}
				resObj.stars = stars;
				//Alert.showErrorTip("签约球童成功");
			}
			else
			{
				AppData.getInstance().youngTrainManager.saleOrgetChildIndexs = [];
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(resObj.retcode) + "");
			}
			return resObj;
		}
		/**
		 * 读取出售球童返回数据
		 * @param data
		 */		
		public static function readSalePlayer(data : ByteArray):Object
		{
			var resObj :Object = new Object();
			resObj.retcode 		= data.readInt();//返回码
			var indexs :Array = AppData.getInstance().youngTrainManager.saleOrgetChildIndexs;
			if(resObj.retcode == 0)
			{
				var baseInfo :YoungTrainInfoVo = AppData.getInstance().youngTrainManager.youngTrainInfoVo;
				baseInfo.caddie = data.readInt();
				var obj :Object = new PlayerChildInfoVo();
				obj.index 		= data.readByte();
				obj.pid 		= data.readInt();
				obj.growbegin 	= data.readUnsignedInt();
				obj.grow 		= data.readInt();
				if(obj.pid > 0)
				{
					indexs.splice(indexs.indexOf(obj.index,1));
					AppData.getInstance().youngTrainManager.addPlayerChild(obj);
				}
				//Alert.showErrorTip("出售球童成功");
			}
			else
			{
				AppData.getInstance().youngTrainManager.saleOrgetChildIndexs = [];
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(resObj.retcode) + "");
			}
			return resObj;
		}
		/**
		 *读取快速成长返回数据 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readGrowAcc(data : ByteArray):Object
		{
			var resObj :Object = new Object();
			resObj.retcode 		= data.readInt();//返回码
			if(resObj.retcode == 0)
			{
				resObj.index = data.readByte();
				AppData.getInstance().youngTrainManager.setPlayerChildComplete(resObj.index);
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(resObj.retcode) + "");
			}
			return resObj;
		}
		/**
		 *读取增加生产球童次数返回数据 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function readAddGrowNum(data : ByteArray):Object
		{
			var resObj :Object = new Object();
			resObj.retcode 		= data.readInt();//返回码
			if(resObj.retcode == 0)
			{
				var baseInfo :YoungTrainInfoVo = AppData.getInstance().youngTrainManager.youngTrainInfoVo;
				baseInfo.caddiecount = data.readInt();
				baseInfo.caddie = data.readInt();
				var obj :Object = new PlayerChildInfoVo();
				obj.index 		= data.readByte();
				obj.pid 		= data.readInt();
				obj.growbegin 	= data.readUnsignedInt();
				obj.grow 		= data.readInt();
				if(obj.pid > 0)
				{
					AppData.getInstance().youngTrainManager.addPlayerChild(obj);
				}
			}
			else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(resObj.retcode) + "");
			}
			return resObj;
		}
	}
}